// 冗余连接

import Foundation

var parent = [Int]()

func findRedundantConnection(_ edges: [[Int]]) -> [Int] {
    if edges.count == 0 {
        return []
    }
    parent = [Int](repeating: 0, count: edges.count + 1)
    for i in 0..<edges.count {
        parent[i] = i
    }
    for (_, element) in edges.enumerated() {
        if find(node: element[0]) == find(node: element[1]) {
            return element
        } else {
            union(node1: element[0], node2: element[1])
        }
    }
    return []
}

func union(node1: Int, node2: Int) {
    parent[find(node: node1)] = find(node: node2)
}

func find(node: Int) -> Int {
    if parent[node] != node {
        parent[node] = find(node: parent[node])
    }
    return parent[node]
}

